package org.scohen.juploadr.uploadapi.flickrrest.authentication;

import org.eclipse.swt.widgets.Shell;
import org.scohen.juploadr.app.authentication.AuthorizationDialog;
import org.scohen.juploadr.ui.ReusableUIFactory;
import org.scohen.juploadr.uploadapi.CommunicationException;
import org.scohen.juploadr.uploadapi.ImageUploadApi;
import org.scohen.juploadr.uploadapi.ProtocolException;
import org.scohen.juploadr.uploadapi.flickrrest.Messages;
import org.scohen.juploadr.uploadapi.flickrrest.RestFlickrApi;
import org.scohen.juploadr.uploadapi.flickrrest.StringSigner;
import org.scohen.juploadr.uploadapi.flickrrest.tags.GetListUser;
import org.scohen.juploadr.uploadapi.flickrrest.upload.GetUploadStatus;
import org.scohen.juploadr.util.JUploadrBrowserLauncher;

public class FlickrAuthEventDirector {
    private static final String PERMS = "write"; //$NON-NLS-1$
    private static final String LINK_BASE = "http://flickr.com/services/auth/?api_key=" //$NON-NLS-1$
            + RestFlickrApi.FLICKR_API_KEY + "&perms=" + PERMS; //$NON-NLS-1$
    private String frob;

    public FlickrAuthEventDirector() {

    }

    public boolean execute(FlickrAccount info) throws ProtocolException, CommunicationException {

        ImageUploadApi api = RestFlickrApi.getInstance();
        if (!info.isAuthenticated()) {

            GetFrob getFrob = new GetFrob();
            getFrob.execute();
            frob = getFrob.getFrob();
            info.setFrob(frob);
            Shell shell = ReusableUIFactory.getInstance().getDisplay().getActiveShell();
            AuthorizationDialog authDialog = new AuthorizationDialog(Messages
                    .getString("juploadr.flickrrest.authdialog.line1"), //$NON-NLS-1$
                    Messages.getString("juploadr.flickrrest.authdialog.line2"), //$NON-NLS-1$
                    Messages.getString("juploadr.flickrrest.authdialog.line3"), //$NON-NLS-1$
                    Messages.getString("juploadr.flickrrest.button.authorize"), shell); //$NON-NLS-1$
            boolean shouldAuth = authDialog.open();

            if (!shouldAuth) {
                return false;
            } else {
                // need to build the link, show the web browser, then the
                // continue dialog.
                String authLink = buildAuthLink();

                JUploadrBrowserLauncher.openURL(authLink, Messages
                        .getString("juploadr.flickrrest.authdialog.browser.not.found"));

                AuthorizationDialog continueDialog = new AuthorizationDialog(Messages
                        .getString("juploadr.flickrrest.continuedialog.line1"), //$NON-NLS-1$
                        Messages.getString("juploadr.flickrrest.continuedialog.line2"), //$NON-NLS-1$
                        Messages.getString("juploadr.flickrrest.continuedialog.line3"), //$NON-NLS-1$
                        Messages.getString("juploadr.flickrrest.button.complete-auth"), shell); //$NON-NLS-1$
                if (!continueDialog.open()) {
                    return false;
                } else {
                    // now we get the motherfucking token
                    // ps. Phew!
                    GetToken getToken = new GetToken(frob);
                    getToken.execute();
                    // if we're here, everything is ok.
                    info.setToken(getToken.getToken());
                    info.setAuthenticated(getToken.getToken() != null);
                    api.setCurrentAccount(info);

                }
            }
        }

        if (ImageUploadApi.getInstance().isCurrentAccountAuthenticated()) {
            // sweet, we have a token, let's get the rest of the info...

            GetUploadStatus gus = new GetUploadStatus(info);
            gus.execute();
            ImageUploadApi.getInstance().setCurrentAccount(info);
            
        }
        return true;
    }

    private String buildAuthLink() {
        StringBuffer link = new StringBuffer(LINK_BASE);
        link.append("&frob="); //$NON-NLS-1$
        link.append(frob);
        link.append("&api_sig="); //$NON-NLS-1$
        StringBuffer apiSig = new StringBuffer(RestFlickrApi.FLICKR_SHARED_SECRET);
        apiSig.append("api_key"); //$NON-NLS-1$
        apiSig.append(RestFlickrApi.FLICKR_API_KEY);
        apiSig.append("frob"); //$NON-NLS-1$
        apiSig.append(frob);
        apiSig.append("perms"); //$NON-NLS-1$
        apiSig.append(PERMS);

        link.append(StringSigner.md5(apiSig.toString()));
        return link.toString();
    }
}
